package com.monkeyboy.mongo_demo;

import com.mongodb.client.AggregateIterable;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.monkeyboy.util.MongoDBAuthUtil;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author Gavin
 * @date 2021.01.11 11:10
 */
public class DateOperation {
    public static void main(String[] args) {
        DateOperation dateOperation = new DateOperation();
        dateOperation.selectDocumentAggregateDateFormat();
    }

    /**
     * 显示指定格式的日期
     * db.java_coll.aggregate([{$match:{username:'test1'}},{$project:{_id:0,'custom_format':{$dateToString:{format:'%Y年%m月%d日 %H:%M:%S',date:'$birth'}}}}])
     * 注意：通过format直接格式化的时间是utc时间，一般不建议直接格式化，而是通过java来进行格式化时间
     */
    public void selectDocumentAggregateDateFormat() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //第一个{}
        Document username = new Document();
        username.put("username", "test1");
        Document match = new Document();
        match.put("$match", username);
        //第二个{}
        Document format = new Document();
        //注意：通过format直接格式化的时间是utc时间
        format.put("format", "%Y年%m月%d日 %H:%M:%S");
        format.put("date", "$birth");

        Document dateToString = new Document();
        dateToString.put("$dateToString", format);

        Document field = new Document();
        field.put("_id", 0);
        field.put("custom_format", dateToString);
        Document project = new Document();
        project.put("$project", field);

        List<Document> list = new ArrayList<>();
        //注意list add的顺序代表着match的意以，如果match在group之前则代表where含义
        //如果在group之后则代表having含义
        list.add(match);
        list.add(project);
        AggregateIterable<Document> documents = collection.aggregate(list);
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("custom_format"));
        }
    }

    /**
     * 日期查询$gt
     */
    public void selectDocumentDateGt() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        Date date = DateUtil.stringToDate("yyyy-MM-dd HH:mm:ss", "2021-01-11 00:00:00");
        FindIterable<Document> documents = collection.find(Filters.gt("birth", date));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags") + "\t" + doc.get("age") + "\t" + DateUtil.dateToString("yyyy-MM-dd HH:mm:ss", (Date) doc.get("birth")));
        }
    }

    /**
     * 查询指定日期的格式
     */
    public void selectDocumentDateUserEq() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        Date date = DateUtil.stringToDate("yyyy-MM-dd HH:mm:ss", "2021-01-11 11:41:31");
        FindIterable<Document> documents = collection.find(Filters.eq("birth", date));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags") + "\t" + doc.get("age") + "\t" + DateUtil.dateToString("yyyy-MM-dd HH:mm:ss", (Date) doc.get("birth")));
        }
    }

    /**
     * 插入指定日期
     */
    public void insertDocumentCustomDate() {
        Date date = DateUtil.stringToDate("yyyy-MM-dd HH:mm:ss", "2021-01-11 11:41:31");
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        Document doc = new Document();
        doc.put("username", "test2");
        doc.put("age", 40);
        doc.put("tags", Arrays.asList("js", "react"));
        doc.put("birth", date);
        collection.insertOne(doc);
    }

    /**
     * 插入系统当前时间
     */
    public void insertDocumentSystemDate() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        Document doc = new Document();
        doc.put("username", "test1");
        doc.put("age", 30);
        doc.put("tags", Arrays.asList("css", "html"));
        doc.put("birth", new Date());
        collection.insertOne(doc);
    }
}
